package com.aote.gradleproject.pojo;

import com.aote.gradleproject.util.Json;
import org.json.JSONObject;

import javax.xml.ws.Service;
import java.io.Serializable;

/**
 * 统一返回类
 * @author 张琪
 * @date 2021/7/20 11:23
 */
public class JsonResult implements Serializable {
    private int statusCode;
    private String message;
    private ServiceResult data;
    private Throwable error;

    public JsonResult(){}

    public JsonResult(int statusCode, String message, ServiceResult data) {
        this.statusCode = statusCode;
        this.message = message;
        this.data = data;
    }

    public JsonResult(int statusCode, String message, Throwable error){
        this.statusCode = statusCode;
        this.message = message;
        this.error = error;
    }


    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(ServiceResult data) {
        this.data = data;
    }

    public Throwable getError() {
        return error;
    }

    public void setError(Throwable error) {
        this.error = error;
    }

    public static JsonResult success(ServiceResult data) {
        return new JsonResult(200, "success", data);
    }

    public static JsonResult fail(Throwable error) {
        return new JsonResult(500, error.getMessage(),error);
    }

    @Override
    public String toString() {
        return Json.object2Json(this).toString();
    }
}
